﻿Imports FastReport.Report
Imports DevExpress.XtraGrid.Views.Grid
Imports MySql.Data.MySqlClient
Public Class frmEditRepairForm
    Public Primary_Key As Integer = -1
    Public Primary_Detail_Key = -1
    Public Curr_RowID As Integer = -1
    Public DocNO As Integer = 0
    Dim Strsql As String
    Dim _mysql As New ClsSQLhelper
    Public Report_Name As String
    Public on_load As Boolean = True


    Dim ClsMysql As New ClsSQLhelper
    Dim MyConnq As New MySql.Data.MySqlClient.MySqlConnection
    Dim DA As MySql.Data.MySqlClient.MySqlDataAdapter

    Dim MyCmdBuilder As MySql.Data.MySqlClient.MySqlCommandBuilder
    Dim MySQLCMD As MySql.Data.MySqlClient.MySqlCommand
    Dim DS As New DataSet
    Dim DT As New DataTable
    Dim DR As DataRow

#Region "Function"

    Sub Load_Department()
        Try
            Dim DT_Department As New DataTable
            Strsql = "Select dep_id,dep_name from departments order by dep_name "
            DT_Department = _mysql.GetMYSQLDataTable(Strsql, "departments")
            With CboDepartments

                .DataSource = DT_Department
                .DisplayMember = "dep_name"
                .ValueMember = "dep_id"
                .SelectedValue = DT_Department.Rows(0).Item("dep_id")
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Department"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub

    Sub Load_repair_place()
        Try
            Dim DT_Department As New DataTable
            Strsql = "Select repair_place_id,repair_place_name from repair_place  "
            DT_Department = _mysql.GetMYSQLDataTable(Strsql, "repair_place")
            With CboRepairPlace

                .DataSource = DT_Department
                .DisplayMember = "repair_place_name"
                .ValueMember = "repair_place_id"
                .SelectedValue = DT_Department.Rows(0).Item("repair_place_id")
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_repair_place"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Sub Load_Data()
        Try
            Strsql = "select * from repair_order where repair_order_id = " & Primary_Key
            Dim DT_Repair As New DataTable
            DT_Repair = _mysql.GetMYSQLDataTable(Strsql, "repair_order")

            CboBudgetYear.Text = DT_Repair.Rows(0).Item("repair_budget_year").ToString
            CboComputer.SelectedValue = DT_Repair.Rows(0).Item("repair_computer")
            CboDepartments.SelectedValue = DT_Repair.Rows(0).Item("repair_department")
            CboEmployee.SelectedValue = DT_Repair.Rows(0).Item("repiar_staff")
            CboRepairPlace.SelectedValue = DT_Repair.Rows(0).Item("repair_place")
            CboStatus.SelectedValue = DT_Repair.Rows(0).Item("repair_status")

            TxtDocNo.Text = DT_Repair.Rows(0).Item("repair_order_no").ToString
            TxtImagepart.Text = DT_Repair.Rows(0).Item("imagepart").ToString

            Memo_repair_note.EditValue = DT_Repair.Rows(0).Item("repair_remark").ToString
            Memo_Survey_Note.EditValue = DT_Repair.Rows(0).Item("repair_survey_note").ToString
            Memo_Technician_Note.EditValue = DT_Repair.Rows(0).Item("repair_technician_note").ToString

            DtpFinishDate.DateTime = DT_Repair.Rows(0).Item("repair_finish_date")
            DTPReceiveDate.DateTime = DT_Repair.Rows(0).Item("repair_order_date")
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Data"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Sub Load_Order_Status()
        Try
            Dim DT_Department As New DataTable
            Strsql = "Select repair_status_id,repair_status_name from repair_status "
            DT_Department = _mysql.GetMYSQLDataTable(Strsql, "repair_status")
            With CboStatus

                .DataSource = DT_Department
                .DisplayMember = "repair_status_name"
                .ValueMember = "repair_status_id"
                .Text = DT_Department.Rows(0).Item("repair_status_name")
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Order_Status"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Sub Load_BudgetYear()

        Try

            Dim MinYear As Integer
            MinYear = 2542

            If MinYear = 0 Then
                Me.CboBudgetYear.Items.Add(Now.Year)
            Else
                For i As Integer = (Now.Year + 543) To MinYear Step -1
                    Me.CboBudgetYear.Items.Add(i)
                    i = +i
                Next
            End If
            Me.CboBudgetYear.Text = (Now.Year + 543)
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_BudgetYear"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Sub Load_Employee()
        Try
            Dim DT_Department As New DataTable
            Strsql = "Select emp_id,emp_fname,emp_lname,concat(emp_fname,emp_lname) as fullname,active from employee  order by emp_fname,emp_lname "
            DT_Department = _mysql.GetMYSQLDataTable(Strsql, "employee")
            With CboEmployee

                .DataSource = DT_Department
                .DisplayMember = "fullname"
                .ValueMember = "emp_id"
                .Text = DT_Department.Rows(0).Item("fullname")
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Employee"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Sub Load_Computer()
        Try

            Dim DT_Department As New DataTable
            Strsql = "Select computername,id from computerinfo where department = " & Me.CboDepartments.SelectedValue & "  order by computername"
            DT_Department = _mysql.GetMYSQLDataTable(Strsql, "computerinfo")
            With CboComputer

                .DataSource = DT_Department
                .DisplayMember = "computername"
                .ValueMember = "id"
                .Text = DT_Department.Rows(0).Item("computername")
            End With
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_Computer"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Function Get_NewOrderNO() As String
        Try
            Dim LasID As Integer = 0
            Dim MyOrderName As String = ""
            Strsql = "Select ifnull(repair_order_id,0) as repair_order_id from repair_order where month(repair_order_date) = " & Now.Month & " and year(repair_order_date) = " & Now.Year & " order by repair_order_id desc limit 1"

            Dim DT_Order As DataTable
            DT_Order = _mysql.GetMYSQLDataTable(Strsql, "repair_order")
            LasID = Val(DT_Order.Rows(0).Item("repair_order_id"))
            MyOrderName = "IT-R" & (Now.Year + 543).ToString & "-" & Now.Month.ToString("0#") & "/"
            If Len((LasID + 1).ToString) = 1 Then
                MyOrderName = MyOrderName & "0" & (LasID + 1).ToString

            End If
            Return MyOrderName
        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Get_NewOrderNO"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try

    End Function
    Sub Load_RepairDetail()
        Try
            Strsql = "Select * from repair_detail where repair_order_id = " & Primary_Key
            DA = New MySql.Data.MySqlClient.MySqlDataAdapter(Strsql, MyConnq)
            MyConnq.ConnectionString = ClsSQLhelper.MySQLConnectionStr
            MyCmdBuilder = New MySqlCommandBuilder(DA)

            'DS = New DataSet("DS_repair_detail")
            DS = New WHISDS

            DA.Fill(DS)

            With GridControl1
                .DataSource = DS.Tables(0)
                TxtOrderNO.NullText = Primary_Key

            End With
        Catch ex As Exception

        End Try
    End Sub
#End Region
    Private Sub frmEditRepairForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Load_Employee()
        Load_BudgetYear()
        Load_Order_Status()
        Load_Department()
        Load_repair_place()
        Load_Computer()
        Load_RepairDetail()
        Me.DTPReceiveDate.DateTime = Now
        Me.DtpFinishDate.DateTime = Now
        Load_Data()
        on_load = False
    End Sub

    Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
        Me.Dispose()
    End Sub

    Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        Try
            Strsql = "Update repair_order set repair_order_no='" & Me.TxtDocNo.Text & "',repair_computer=" & CboComputer.SelectedValue & ",repair_budget_year='" & CboBudgetYear.Text & "',repair_order_date='"
            Strsql = Strsql & Pn_Framework.MysqlDateTimeFormat(DTPReceiveDate.DateTime, False) & "',repair_department=" & CboDepartments.SelectedValue & ",repiar_staff=" & Me.CboEmployee.SelectedValue & ",repair_finish_date='" & Pn_Framework.MysqlDateTimeFormat(DtpFinishDate.DateTime, False) & "'"
            Strsql = Strsql & ",repair_place=" & CboRepairPlace.SelectedValue & ",repair_status=" & CboStatus.SelectedValue & ",repair_remark='" & Memo_repair_note.EditValue & "',repair_survey_note='" & Memo_Survey_Note.EditValue & "',repair_technician_note='" & Memo_Technician_Note.EditValue & "',imagepart='" & Me.TxtImagepart.Text & "'"
            Strsql = Strsql & " where repair_order_id = " & Primary_Key

            If _mysql.MySQLExecute(Strsql) >= 0 Then
                MsgBox("บันทึกข้อมูลเอกสารเรียบร้อย", MsgBoxStyle.Information, "ผลการบันทึกข้อมูล")
                frmPRList.Load_Data()
            Else
                With frmDebug
                    .lblFormName.Text = Me.Name
                    .lblFunctionName.Text = "BtnSave_Click"
                    .MemoErr_Description.Text = "กรุณาตรวจสอบคำสั่ง sql"
                    .MemoSQL.Text = Strsql
                    .ShowDialog()
                End With
            End If

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Get_NewOrderNO"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub

    Private Sub CboDepartments_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboDepartments.SelectedIndexChanged
        If on_load = True Then Exit Sub
        Load_Computer()
    End Sub

End Class